﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using cMES3001.Services;
using Newtonsoft.Json;
using xClient.Framework;
using xClient.Framework.StaticResource;

namespace cMES3001
{
    public partial class cEdit : Form
    {
        public cEdit(string command, params object[] values)
        {
            InitializeComponent();

            //if (command.Equals("ADD"))
            //{
            //    this.Text = "新增供应商";
            //    this.txtVendorCode.Text = "";
            //    this.txtVendorName.Text = "";
            //    this.txtVendorDesc.Text = "";
            //    this.txtVendorAddr.Text = "";
            //    this.txtVendorPhone.Text = "";
            //    this.txtVendorContact.Text = "";
            //}
            if (command.Equals("EDIT"))
            {
                this.Text = "修改材料信息";
                this.txtSerialNumber.Enabled = false;
                this.txtSerialNumber.Text = values[0].ToString();
                this.txtKeyPartNo.Text = values[1].ToString();
                this.txtKps.Text = values[2].ToString();
                this.txtQty.Text = values[5].ToString();
                this.txtRemark.Text = values[9].ToString();
                this.txtLine.Text = values[3].ToString();
                this.txtGroup.Text = values[4].ToString();
                this.txtAlternative.Text = values[7].ToString();
                this.txtIsValid.Text = values[8].ToString();
            }

            btnEdit.Click += delegate
            {
                #region ADD
                if (command.Equals("ADD"))
                {
                    //if (txtVendorCode.Text.Length < 1) { return; }
                    //if (txtVendorName.Text.Length < 1) { return; }
                    //if (txtVendorDesc.Text.Length < 1) { return; }
                    //if (txtVendorAddr.Text.Length < 1) { return; }
                    //if (txtVendorPhone.Text.Length < 1) { return; }
                    //if (txtVendorContact.Text.Length < 1) { return; }

                    //string vm = JsonConvert.SerializeObject(new
                    //{
                    //    vcode = txtVendorCode.Text,
                    //    vname = txtVendorName.Text,
                    //    vdesc = txtVendorDesc.Text,
                    //    vaddr = txtVendorAddr.Text,
                    //    vphone = txtVendorPhone.Text,
                    //    vcontact = txtVendorContact.Text,
                    //    cby = StaticDictionary.Dictionary.UserID,
                    //    mby = StaticDictionary.Dictionary.UserID
                    //});

                    //ExecutionResult result = ServiceMethod.AddVendor(vm);

                    //if (result.Status)
                    //{
                    //    this.DialogResult = DialogResult.OK;
                    //}
                    //else
                    //{
                    //    this.DialogResult = DialogResult.No;
                    //    MessageBox.Show(result.Message, "系统消息");
                    //}
                }
                #endregion

                if (command.Equals("EDIT"))
                {
                    if (txtKeyPartNo.Text.Length < 1) { return; }
                    if (txtKps.Text.Length < 1) { return; }
                    if (txtQty.Text.Length < 1) { return; }
                    if (txtRemark.Text.Length < 1) { return; }

                    string vm = JsonConvert.SerializeObject(new
                    {
                        sn = txtSerialNumber.Text,
                        kpn = txtKeyPartNo.Text,
                        remarks = txtRemark.Text,
                        line = txtLine.Text,
                        group=txtGroup.Text,
                        kpsn= txtKps.Text=="N/A"?txtKeyPartNo.Text:txtKps.Text,
                        qty=txtQty.Text,
                        alternative=txtAlternative.Text,
                        is_valid=txtIsValid.Text,
                        emp = StaticDictionary.Dictionary.UserID
                    });

                    ExecutionResult result = ServiceMethod.UpdateWipKpsBySn(vm);

                    if (result.Status)
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.No;
                        MessageBox.Show(result.Message, "系统消息");
                    }
                }
            };


        }
    }
}
